import { _decorator, Component, instantiate, Node, Prefab, random, v3, Vec3, view } from 'cc';
import { ModuleBase } from './ModuleBase';
import { resMgr } from './Res/ResMgr';
import { PrefabMgr } from './Res/SpecialResMgr';
import { Enemy } from './Enemy';

const { ccclass, property } = _decorator;

@ccclass('EnemyLayers')
export class EnemyLayers extends ModuleBase {
    private _enemyPre: Prefab = null;
    private _enemyTs: Enemy = null;

    onInit(...rest: any[]): void {
        this._enemyPre = resMgr.getMgr<PrefabMgr>("PrefabMgr").getRes<Prefab>("Enemy");
    }
    onLateInit(...rest: any[]): void {
    }

    //产生敌人
    createEnemy() {

        let enemyNode = instantiate(this._enemyPre);
        enemyNode.parent = this.node;
        let maxX = view.getDesignResolutionSize().width * 0.7;
        let minX = view.getDesignResolutionSize().width * 0.45;
        let x = Math.random() * (maxX - minX) + minX;
        enemyNode.setPosition(v3(x, 0, 0));
        this._enemyTs = enemyNode.getComponent(Enemy);
        this._enemyTs.oninit(this);
    }

    //敌人开火射击
    enemyFire(playerNode) {
        let EnemyNode = this.node.getChildByName("Enemy");
        let EnemyTs = EnemyNode.getComponent(Enemy);
        EnemyTs.fire(playerNode);
    }

}


